package com.jian.bus.service.impl;

import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Sales;
import com.jian.bus.mapper.SalesMapper;
import com.jian.bus.service.GoodsService;
import com.jian.bus.service.SalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 剑
 * @since 2020-01-26
 */
@Service
@Transactional
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {
    @Autowired
    GoodsService goodsService;
    @Override
    public boolean save(Sales entity) {
        Goods goods = goodsService.getById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()-entity.getNumber());
        goodsService.updateById(goods);
        return super.save(entity);
    }

    @Override
    public boolean updateById(Sales entity) {
        Goods goods = goodsService.getById(entity.getGoodsid());
        Sales oldSales = this.getBaseMapper().selectById(entity.getId());
        goods.setNumber(goods.getNumber()+oldSales.getNumber()-entity.getNumber());
        goodsService.updateById(goods);
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        Sales oldSales=this.getBaseMapper().selectById(id);
        Goods goods = goodsService.getById(oldSales.getGoodsid());
        goods.setNumber(goods.getNumber()+oldSales.getNumber());
        goodsService.updateById(goods);
        return super.removeById(id);
    }
}
